class SIMPLE_NOTEPAD_3

inherit
	IUP_INTERFACE

create {ANY}
   make

feature {ANY}

	-- attribute to be used inside the menu callbacks.
	ml: IUP_MULTILINE

	read_file (file_name: STRING): STRING
		local
			tfr: PLAIN_TEXT_FILE
			str: STRING
			ms: IUP_MESSAGE
		do
			create str.make_empty
         create tfr.make_open_read(file_name)

			if tfr.is_access_readable then
				from
					tfr.read_line
				until
					tfr.end_of_file
				loop
					str.append(tfr.last_string)
					str.append_character('%N')
					tfr.read_line
				end
				
				str.append(tfr.last_string)
				tfr.close
			else
				str.append("Fail when reading from file: ")
				str.append(file_name)
				create ms.message("Error", str)
			end
			
			Result := str
		end

	write_file (file_name, text: STRING)
		local
			tfw: PLAIN_TEXT_FILE
			ms: IUP_MESSAGE
			str: STRING
		do
			create tfw.make_open_write(file_name)

			if tfw.is_access_writable then
				tfw.put_string(text)
				tfw.close
			else
				create str.make_empty
				str.append("Fail when writing to file: ")
				str.append(file_name)
				create ms.message("Error", str)
			end
		end

	open_cb (widget: IUP_MENU_ITEM): STRING
		local
			status: INTEGER
			rv, name, text: STRING
			fd: IUP_FILE_DIALOG
		do
			create fd.file_dialog
			fd.set_dialog_type("OPEN")
			fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
			fd.set_title("Select a text file")

			rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")

			if rv.is_equal("IUP_NOERROR") then
				status := fd.get_status

				if status.is_equal(0) then
					name := fd.get_value
					text := read_file(name)
					ml.set_value(text)
				end
			end

			fd.destroy
			Result := "IUP_DEFAULT"
		end

	save_as_cb (widget: IUP_MENU_ITEM): STRING
		local
			status: INTEGER
			rv, name, text: STRING
			fd: IUP_FILE_DIALOG
		do
			create fd.file_dialog
			fd.set_dialog_type("SAVE")
			fd.set_ext_filter("Text files|*.txt|All Files|*.*|")
			fd.set_title("Select a text file")

			rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")

			if rv.is_equal("IUP_NOERROR") then
				status := fd.get_status

				if not status.is_equal(-1) then
					name := fd.get_value
					text := ml.get_value
					write_file(name, text)
				end
			end

			fd.destroy
			Result := "IUP_DEFAULT"
		end

	font_cb (widget: IUP_MENU_ITEM): STRING
		local
			status: INTEGER
			rv, ft, selected: STRING
			fd: IUP_FONT_DIALOG
		do
			create fd.font_dialog
			ft := ml.get_font
			fd.set_value(ft)

			rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")

			if rv.is_equal("IUP_NOERROR") then
				status := fd.get_status

				if status.is_equal(1) then
					selected := fd.get_value
					ml.set_font(selected)
				end
			end

			fd.destroy
			Result := "IUP_DEFAULT"
		end

	about_cb (widget: IUP_MENU_ITEM): STRING
		local
			ms: IUP_MESSAGE
		do
			create ms.message("About","    Simple Notepad%N%NAuthors:%N   Gustavo Lyrio%N   Antonio Scuri%N   German Arias")
			Result := "IUP_DEFAULT"
		end

	exit_cb (widget: IUP_MENU_ITEM): STRING
		do
			Result := "IUP_CLOSE"
		end

	make
      local
			i: STRING
			gui: IUP
			v: IUP_VBOX
			w: IUP_DIALOG
			item_exit, item_open, item_save_as, item_font, item_about: IUP_MENU_ITEM
			sep: IUP_MENU_SEPARATOR
			file_menu, format_menu, help_menu, menu: IUP_MENU
			sub_menu_file, sub_menu_format, sub_menu_help: IUP_SUBMENU
		do
			gui := iup_open
			
			create ml.multiline
			ml.expand_both_directions
			
	      create v.vbox({ARRAY[IUP_WIDGET]} << ml >>)

			create item_open.item("Open...")
			item_open.set_hide_mark(True)
			item_open.set_cb_action(agent open_cb)

			create item_save_as.item("Save As...")
			item_save_as.set_hide_mark(True)
			item_save_as.set_cb_action(agent save_as_cb)

			create item_exit.item("Exit")
			item_exit.set_hide_mark(True)
			item_exit.set_cb_action(agent exit_cb)

			create item_font.item("Font...")
			item_font.set_hide_mark(True)
			item_font.set_cb_action(agent font_cb)

			create item_about.item("About...")
			item_about.set_hide_mark(True)
			item_about.set_cb_action(agent about_cb)

			create sep.separator

			create file_menu.menu({ARRAY[IUP_MENU_ELEMENT]} << item_open, item_save_as, sep, item_exit >>)
			create format_menu.menu({ARRAY[IUP_MENU_ELEMENT]} << item_font >>)
			create help_menu.menu({ARRAY[IUP_MENU_ELEMENT]} << item_about >>)

			create sub_menu_file.submenu("File", file_menu)
			create sub_menu_format.submenu("Format", format_menu)
			create sub_menu_help.submenu("Help", help_menu)
			
			create menu.menu({ARRAY[IUP_MENU_ELEMENT]} << sub_menu_file, sub_menu_format, sub_menu_help >>)
			
	      create w.dialog(v)
			w.set_menu_widget(menu)
			w.set_title("Simple Notepad")
			w.set_predefined_size("QUARTER", "QUARTER")
			
	      i := w.show_predefined_xy("IUP_CENTER", "IUP_CENTER")
			w.set_user_size(0, 0)
			
         gui.main_loop
			gui.close
		end

end
